package com.faye.sdk.service;

import io.modelcontextprotocol.server.McpServer;
import io.modelcontextprotocol.server.McpServerFeatures;
import io.modelcontextprotocol.server.McpSyncServer;
import io.modelcontextprotocol.server.transport.WebMvcSseServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.SmartFactoryBean;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class McpSyncServerFactoryBean implements SmartFactoryBean<McpSyncServer> {
    private final WebMvcSseServerTransportProvider webMvcSseServerTransportProvider;

    // The object that this factory bean creates
    @Override
    public McpSyncServer getObject() throws Exception {
        McpSyncServer mcpSyncServer = McpServer.sync(webMvcSseServerTransportProvider)
            .serverInfo("my-server", "1.0.0")
            .capabilities(McpSchema.ServerCapabilities.builder()
                .resources(false, true)  // Enable resource support
                .tools(true)             // Enable tool support
                .prompts(false)           // Enable prompt support
                .logging()               // Enable logging support
                .completions()           // Enable completions support
                .build())
            .build();

        mcpSyncServer.addTool(createSyncToolSpecification());
        return mcpSyncServer;
    }

    // The type of object this factory creates
    @Override
    public Class<?> getObjectType() {
        return McpSyncServer.class;
    }

    // Whether the factory returns singleton instances
    @Override
    public boolean isSingleton() {
        return true; // Return true for singleton, false for prototype
    }

    @Override
    public boolean isEagerInit() {
        return true;
    }

    public McpServerFeatures.SyncToolSpecification createSyncToolSpecification() {
        var schema = """
            {
              "type" : "object",
              "id" : "urn:jsonschema:Operation",
              "properties" : {
                "operation" : {
                  "type" : "string"
                },
                "a" : {
                  "type" : "number"
                },
                "b" : {
                  "type" : "number"
                }
              }
            }
            """;
        return new McpServerFeatures.SyncToolSpecification(
            new McpSchema.Tool("calculator", "Basic calculator", schema),
            (exchange, arguments) -> {
                // Tool implementation
                return new McpSchema.CallToolResult("3", false);
            }
        );
    }
}
